* Programs for Spatial Mismatch estimation

program define marginal_effect_predval

* calculate mean and standard deviation
tabstat ///
`2' ///
if qtr==8, statistics(p5 p10 p25 p50 p75 p90 p95) ///
columns(statistics) format(%12.5g) save

matrix define sum_stat=r(StatTotal)'
matrix list sum_stat

* convert matrix of changes into scalars (do log after difference)
scalar sum_stat_ratio_p05=sum_stat[1,1]
scalar sum_stat_ratio_p10=sum_stat[1,2]
scalar sum_stat_ratio_p25=sum_stat[1,3]
scalar sum_stat_ratio_p50=sum_stat[1,4]
scalar sum_stat_ratio_p75=sum_stat[1,5]
scalar sum_stat_ratio_p90=sum_stat[1,6]
scalar sum_stat_ratio_p95=sum_stat[1,7]

* convert scalars of changes into local variables
local p05_s=sum_stat_ratio_p05
local p10_s=sum_stat_ratio_p10
local p25_s=sum_stat_ratio_p25
local p50_s=sum_stat_ratio_p50
local p75_s=sum_stat_ratio_p75
local p90_s=sum_stat_ratio_p90
local p95_s=sum_stat_ratio_p95

* predict outcome for each percentile
estimates restore jobsearch
margins, at(`2'=(`p05_s' `p10_s' `p25_s' `p50_s' `p75_s' `p90_s' `p95_s')) predict(pr(.,0)) post
matrix marg0=r(b)
estimates restore jobsearch
margins, at(`2'=(`p05_s' `p10_s' `p25_s' `p50_s' `p75_s' `p90_s' `p95_s')) predict(pr(0,0.693147181)) post
matrix marg1=r(b)
estimates restore jobsearch
margins, at(`2'=(`p05_s' `p10_s' `p25_s' `p50_s' `p75_s' `p90_s' `p95_s')) predict(pr(0.693147181,1.098612289)) post
matrix marg2=r(b)
estimates restore jobsearch
margins, at(`2'=(`p05_s' `p10_s' `p25_s' `p50_s' `p75_s' `p90_s' `p95_s')) predict(pr(1.098612289,1.386294361)) post
matrix marg3=r(b)
estimates restore jobsearch
margins, at(`2'=(`p05_s' `p10_s' `p25_s' `p50_s' `p75_s' `p90_s' `p95_s')) predict(pr(1.386294361,1.609437912)) post
matrix marg4=r(b)
estimates restore jobsearch
margins, at(`2'=(`p05_s' `p10_s' `p25_s' `p50_s' `p75_s' `p90_s' `p95_s')) predict(pr(1.609437912,1.791759469)) post
matrix marg5=r(b)
estimates restore jobsearch
margins, at(`2'=(`p05_s' `p10_s' `p25_s' `p50_s' `p75_s' `p90_s' `p95_s')) predict(pr(1.791759469,1.945910149)) post
matrix marg6=r(b)
estimates restore jobsearch
margins, at(`2'=(`p05_s' `p10_s' `p25_s' `p50_s' `p75_s' `p90_s' `p95_s')) predict(pr(1.945910149,2.079441542)) post
matrix marg7=r(b)
estimates restore jobsearch
margins, at(`2'=(`p05_s' `p10_s' `p25_s' `p50_s' `p75_s' `p90_s' `p95_s')) predict(pr(2.079441542,2.197224577)) post
matrix marg8=r(b)
estimates restore jobsearch
margins, at(`2'=(`p05_s' `p10_s' `p25_s' `p50_s' `p75_s' `p90_s' `p95_s')) predict(pr(2.197224577,.)) post
matrix marg9=r(b)

matrix marg_q=[.,0,1,2,3,4,5,6,7,8,9]
matrix marg_p=[sum_stat\marg0\marg1\marg2\marg3\marg4\marg5\marg6\marg7\marg8\marg9]
matrix marg_`1'=[marg_q\marg_p']
matrix list marg_`1', format(%7.4f)

end



program define marginal_effect_predval_grp

* calculate mean and standard deviation
tabstat ///
`2' ///
if qtr==8 /*& grp_c==`3'*/, statistics(p5 p10 p25 p50 p75 p90 p95) ///
columns(statistics) format(%12.5g) save

matrix define sum_stat=r(StatTotal)'
matrix list sum_stat

* convert matrix of changes into scalars (do log after difference)
scalar sum_stat_ratio_p05=sum_stat[1,1]
scalar sum_stat_ratio_p10=sum_stat[1,2]
scalar sum_stat_ratio_p25=sum_stat[1,3]
scalar sum_stat_ratio_p50=sum_stat[1,4]
scalar sum_stat_ratio_p75=sum_stat[1,5]
scalar sum_stat_ratio_p90=sum_stat[1,6]
scalar sum_stat_ratio_p95=sum_stat[1,7]

* convert scalars of changes into local variables
local p05_s=sum_stat_ratio_p05
local p10_s=sum_stat_ratio_p10
local p25_s=sum_stat_ratio_p25
local p50_s=sum_stat_ratio_p50
local p75_s=sum_stat_ratio_p75
local p90_s=sum_stat_ratio_p90
local p95_s=sum_stat_ratio_p95

* predict outcome for each percentile
estimates restore jobsearch
margins, at(`2'=(`p05_s' `p10_s' `p25_s' `p50_s' `p75_s' `p90_s' `p95_s')) predict(pr(.,0)) post
matrix marg0=r(b)
estimates restore jobsearch
margins, at(`2'=(`p05_s' `p10_s' `p25_s' `p50_s' `p75_s' `p90_s' `p95_s')) predict(pr(0,0.693147181)) post
matrix marg1=r(b)
estimates restore jobsearch
margins, at(`2'=(`p05_s' `p10_s' `p25_s' `p50_s' `p75_s' `p90_s' `p95_s')) predict(pr(0.693147181,1.098612289)) post
matrix marg2=r(b)
estimates restore jobsearch
margins, at(`2'=(`p05_s' `p10_s' `p25_s' `p50_s' `p75_s' `p90_s' `p95_s')) predict(pr(1.098612289,1.386294361)) post
matrix marg3=r(b)
estimates restore jobsearch
margins, at(`2'=(`p05_s' `p10_s' `p25_s' `p50_s' `p75_s' `p90_s' `p95_s')) predict(pr(1.386294361,1.609437912)) post
matrix marg4=r(b)
estimates restore jobsearch
margins, at(`2'=(`p05_s' `p10_s' `p25_s' `p50_s' `p75_s' `p90_s' `p95_s')) predict(pr(1.609437912,1.791759469)) post
matrix marg5=r(b)
estimates restore jobsearch
margins, at(`2'=(`p05_s' `p10_s' `p25_s' `p50_s' `p75_s' `p90_s' `p95_s')) predict(pr(1.791759469,1.945910149)) post
matrix marg6=r(b)
estimates restore jobsearch
margins, at(`2'=(`p05_s' `p10_s' `p25_s' `p50_s' `p75_s' `p90_s' `p95_s')) predict(pr(1.945910149,2.079441542)) post
matrix marg7=r(b)
estimates restore jobsearch
margins, at(`2'=(`p05_s' `p10_s' `p25_s' `p50_s' `p75_s' `p90_s' `p95_s')) predict(pr(2.079441542,2.197224577)) post
matrix marg8=r(b)
estimates restore jobsearch
margins, at(`2'=(`p05_s' `p10_s' `p25_s' `p50_s' `p75_s' `p90_s' `p95_s')) predict(pr(2.197224577,.)) post
matrix marg9=r(b)

matrix marg_q=[.,0,1,2,3,4,5,6,7,8,9]
matrix marg_p=[sum_stat\marg0\marg1\marg2\marg3\marg4\marg5\marg6\marg7\marg8\marg9]
matrix marg_`1'=[marg_q\marg_p']
matrix list marg_`1', format(%7.4f)

end
